import { createSlice } from '@reduxjs/toolkit'
import dayjs from 'dayjs'

export const ticketSlice = createSlice({
  name: 'ticket',
  initialState: {
    start:'北京',
    end:'上海',
    date:dayjs().format("YYYY-MM-DD"),
    searchHistory:[]
  },
  reducers: {
        jh(state){
            let temp=state.start
            state.start=state.end
            state.end=temp
        },
        setDate(state,actions){
            state.date=dayjs(actions.payload).format("YYYY-MM-DD")
        },
        setCity(state,actions){
            state[actions.payload.propName]=actions.payload.city
        },
        clearHistory(state){
            state.searchHistory=[]
        },
        addHistory(state,actions){
            state.searchHistory.unshift(actions.payload)
            const set=new Set(state.searchHistory)
            state.searchHistory=Array.from(set)
        }
  }
})
// 每个 case reducer 函数会生成对应的 Action creators
export const { jh,setDate,setCity,clearHistory,addHistory } = ticketSlice.actions

export default ticketSlice.reducer